/*
 * Created by JFormDesigner on Sun Jun 06 21:39:53 CST 2021
 */

package com.J1ay.carsys.gui.User;

import com.J1ay.carsys.dao.BowerMapper;
import com.J1ay.carsys.dto.LoginInput;
import com.J1ay.carsys.dto.LoginOutput;
import com.J1ay.carsys.gui.Emp.Main;
import com.J1ay.carsys.gui.User.Dialog.AddBower;
import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Emp;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;
import com.J1ay.carsys.utils.Common;
import com.J1ay.carsys.utils.MybatisUtils;

import java.awt.*;
import java.awt.event.*;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class UserLogin extends JFrame {
    EmpService empService = new EmpServiceImpl();
    public UserLogin() {
        initComponents();
    }
    public void start() {
        this.setVisible(true);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }

    private void loginActionPerformed(final ActionEvent e) {
        // TODO add your code here
        String phone = this.phone.getText();
        try {
            if (!Common.checkPhone(phone)){
                new Warning(this, "手机号码格式有异！").start();
            }
            else{
                final Result Result = empService.getBowerByPhone(phone);
                System.out.println(Result);
                if(200 == Result.getStatus()) {
                    System.out.println(Result.getData());
                    new Success(this,"登录成功！").start();
                    new UserMain((Bower) Result.getData()).start();
                    // 关闭窗口
                    JButton jButton = (JButton) e.getSource();
                    Container container = jButton.getParent();
                    while (!(container instanceof JFrame)) {
                        container = container.getParent();
                    }
                    ((JFrame)container).dispose();
                }
                else {
                    new Warning(this,Result.getMsg()).start();
                }
            }
        } catch (Exception e1){
            e1.printStackTrace();
            new Warning(this, "手机号码格式有异！").start();
            this.phone.setText("");
        }

    }

    private void phoneKeyPressed(final KeyEvent e) {
        // TODO add your code here
        if(e.getKeyChar() == KeyEvent.VK_ENTER ) {
            String phone = this.phone.getText();
            try {
                if (!Common.checkPhone(phone)){
                    new Warning(this, "手机号码格式有异！").start();
                    this.phone.setText("");
                }
                else{
                    final Result Result = empService.getBowerByPhone(phone);
                    System.out.println(Result);
                    if(200 == Result.getStatus()) {
                        System.out.println(Result.getData());
                        new Success(this,"登录成功！").start();
//
                        new UserMain((Bower)Result.getData()).start();
                        // 关闭窗口
                       JTextField jTextField = (JTextField) e.getSource();
                        Container container = jTextField.getParent();
                        while (!(container instanceof JFrame)) {
                            container = container.getParent();
                        }
                        ((JFrame)container).dispose();
                    }
                    else {
                        new Warning(this,Result.getMsg()).start();
                        this.phone.setText("");
                    }
                }
            } catch (Exception e1){
                e1.printStackTrace();
                new Warning(this, "手机号码格式有异！").start();
                this.phone.setText("");
            }
        }
    }

    private void loginKeyPressed(KeyEvent e) {
        // TODO add your code here
    }

    private void button1ActionPerformed(ActionEvent e) {
        // TODO add your code here
        new AddBower(this, MybatisUtils.getSqlSession().getMapper(BowerMapper.class).countBower()).start();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        phone = new JTextField();
        login = new JButton();
        button1 = new JButton();

        //======== this ========
        setTitle("\u5c0f\u6c7d\u8f66\u51fa\u79df\u7ba1\u7406\u7cfb\u7edf");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u8bf7\u8f93\u5165\u60a8\u7684\u624b\u673a\u53f7\u7801");

        //---- phone ----
        phone.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                phoneKeyPressed(e);
            }
        });

        //---- login ----
        login.setText("\u8fdb\u5165");
        login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                loginActionPerformed(e);
            }
        });
        login.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                loginKeyPressed(e);
            }
        });

        //---- button1 ----
        button1.setText("\u6ce8\u518c");
        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                button1ActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(0, 109, Short.MAX_VALUE)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 183, GroupLayout.PREFERRED_SIZE)
                    .addGap(106, 106, 106))
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(74, 74, 74)
                            .addComponent(phone, GroupLayout.PREFERRED_SIZE, 250, GroupLayout.PREFERRED_SIZE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(93, 93, 93)
                            .addComponent(login)
                            .addGap(67, 67, 67)
                            .addComponent(button1)))
                    .addContainerGap(74, Short.MAX_VALUE))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(phone, GroupLayout.PREFERRED_SIZE, 47, GroupLayout.PREFERRED_SIZE)
                    .addGap(30, 30, 30)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(button1)
                        .addComponent(login))
                    .addContainerGap(53, Short.MAX_VALUE))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JTextField phone;
    private JButton login;
    private JButton button1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
